package com.hpe.wx.performance.redis.ctrl;

import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.net.MalformedURLException;

import javax.management.AttributeNotFoundException;
import javax.management.InstanceNotFoundException;
import javax.management.MBeanException;
import javax.management.MBeanServer;
import javax.management.MBeanServerConnection;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.ReflectionException;
import javax.management.openmbean.CompositeDataSupport;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;

public class TestRemoteJMX {
	public static void main(String[] args) {

		try {
			JMXServiceURL serviceURL = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://MYDPH0075.hpswlabs.adapps.hp.com:29601/jmxrmi"); 
			
			JMXConnector conn = JMXConnectorFactory.connect(serviceURL, null);
			
			MBeanServerConnection mbsc = conn.getMBeanServerConnection();

			MBeanServer server = ManagementFactory.getPlatformMBeanServer();

			ObjectName operatingObjName = new ObjectName("java.lang:type=OperatingSystem");

			//ObjectName oname = new ObjectName(ManagementFactory.RUNTIME_MXBEAN_NAME);
			
			Long totalPhysicalMemorySize = (Long)mbsc.getAttribute(operatingObjName, "TotalPhysicalMemorySize")/1024 ;
			
			Long freePhysicalMemorySize = (Long)mbsc.getAttribute(operatingObjName, "FreePhysicalMemorySize")/1024 ;
			
			Double systemCpuLoad = Double.parseDouble(mbsc.getAttribute(operatingObjName, "SystemCpuLoad")+"")*100;

			Double processCpuLoad = Double.parseDouble(mbsc.getAttribute(operatingObjName, "ProcessCpuLoad")+"")*100 ;

			System.out.println("TotalPhysicalMemorySize: " + totalPhysicalMemorySize);
			System.out.println("FreePhysicalMemorySize: " + freePhysicalMemorySize);
			System.out.println("The memery usage is: " + (1-freePhysicalMemorySize*1.0/totalPhysicalMemorySize)*100 + "%");
			System.out.println("SystemCpuLoad: " +systemCpuLoad + "%");
			System.out.println("ProcessCpuLoad: " + processCpuLoad + "%")  ; 
			
			ObjectName jvmObjName = new ObjectName("java.lang:type=Memory");
			CompositeDataSupport heapMemoryUsage = (CompositeDataSupport) mbsc.getAttribute(jvmObjName, "HeapMemoryUsage")  ;
			long init = Long.parseLong(heapMemoryUsage.get("init")+"");             //初始大小
			long max = Long.parseLong(heapMemoryUsage.get("max")+"") ;
			long committed = Long.parseLong(heapMemoryUsage.get("committed")+"");
			long used = Long.parseLong(heapMemoryUsage.get("used")+"");
			
			System.out.println("The jvm heapmemery usage is: " + used*1.0/committed*100 +"%");
			
			
			CompositeDataSupport nonHeapMemoryUsage = (CompositeDataSupport)mbsc.getAttribute(jvmObjName, "NonHeapMemoryUsage")  ;
			
			System.out.println(nonHeapMemoryUsage);
		} catch (MalformedObjectNameException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (AttributeNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstanceNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MBeanException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ReflectionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}
